package com.tzy.dishes.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzy.common.constant.TPage;
import com.tzy.common.entity.Dishes;
import com.tzy.dishes.mapper.DishesMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
 * <p>
 * 菜品详细表 服务实现类
 * </p>
 *
 * @author 谭同学
 * @since 2022-03-01
 */
@Service
public class DishesService extends ServiceImpl<DishesMapper, Dishes> implements IDishesService {
    @Resource
    private DishesMapper dishesMapper;

    /**
     * 根据菜品名称模糊查询菜品列表
     * @param dishesName
     * @param pageNum
     * @param size
     * @return
     */
    public Page<Dishes> getFuzzyQueryDishesPageByDishesName(String dishesName, Integer pageNum, Integer size) {
        if( pageNum!=null && size!=null ){
            if(pageNum <= 0 ){
                pageNum = 0 * size;
            }else{
                pageNum=(pageNum-1)*size;
            }
        }
        int total = dishesMapper.findTotal();
        System.out.println("total="+total+",pageNum="+pageNum);
        if( total - (pageNum+1)*size <0 ){
            pageNum = pageNum-1;
        }
        List<Dishes> list = dishesMapper.findFuzzyQueryDishesListPageByDishesName(dishesName, pageNum, size);
        Page<Dishes> page = new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setCurrent(pageNum);
        return page;
    }


    public TPage<Map<String, Object>> getAllDishesListMapsPageByDishesName(String dishesName, Integer pageNum, Integer size) {
        if( pageNum!=null && size!=null ){
            if(pageNum <= 0 ){
                pageNum = 0 * size;
            }else{
                pageNum=(pageNum-1)*size;
            }
        }
        int total = dishesMapper.findTotal();
        System.out.println("total="+total+",pageNum="+pageNum);
        if( total - (pageNum+1)*size <0 ){
            pageNum = pageNum-1;
        }
        List<Map<String, Object>> listMaps = dishesMapper.findAllDishesListMapsPageByDishesName(dishesName, pageNum, size);
        TPage<Map<String, Object>> page = new TPage<>();
        page.setRecordsMap(listMaps);
        page.setTotal(total);
        page.setCurrent(pageNum);
        return page;
    }

    /**
     * 根据 Wrapper，查询一条记录 <br/>
     * <p>结果集，如果是多个会抛出异常，随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
     *
     */
    @Cacheable(cacheNames = "details")
    public Dishes getDishesDetailsLimit1(int dishesId) {
        QueryWrapper<Dishes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dishesId);
        return super.getOne(queryWrapper);
    }

    @Cacheable(cacheNames = "page")
    public Page<Dishes> getListByTypeIdPage(int dishesTypeId, Integer pageNum, Integer size){
        if( pageNum!=null && size!=null ){
            if(pageNum <= 0 ){
                pageNum = 0 * size;
            }else{
                pageNum=(pageNum-1)*size;
            }
        }
        int total = dishesMapper.findTotal();
        System.out.println("total="+total+",pageNum="+pageNum);
        if( total - (pageNum+1)*size <0 ){
            pageNum = pageNum-1;
        }
        List<Dishes> list = dishesMapper.findAllListByTypeIdPage(dishesTypeId, pageNum, size);
        Page<Dishes> page = new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setCurrent(pageNum);
        return page;
    }
    /**
     * 插入一条记录（选择字段，策略插入）
     *
     * @param entity 实体对象
     */
    @Override
    @Transactional( rollbackFor = {Exception.class} )//开启事务,遇到异常就回滚
    @CacheEvict(value = {"page","list","details"},allEntries = true)//删除缓存
    public boolean save(Dishes entity) {
        return super.save(entity);
    }

    @Override
    @Transactional( rollbackFor = {Exception.class} )//开启事务,遇到异常就回滚
    @CacheEvict(allEntries = true)
    public boolean updateById(Dishes entity) {
        return super.updateById(entity);
    }
    @Cacheable(cacheNames = "list")
    public List<Dishes> getDishesListByShopId(int shopId) {
        return dishesMapper.findDishesListByShopId(shopId);
    }
    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link QueryWrapper}
     */
    @Override
    public List<Dishes> list(Wrapper<Dishes> queryWrapper) {
        return super.list(queryWrapper);
    }

}
